﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
//using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using TSL_Tools.SqlTools;
using ReportingEngine;
using System.IO;
using System.Windows.Forms.DataVisualization.Charting;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;


namespace ReportGenerator
{
    public partial class TSL_ReportGenerator
    {
        public Table GenerateTable()
        {
            Table table1 = new Table();

            #region -- TABLE properties -----
            TableProperties tableProperties1 = new TableProperties();
            TableStyle tableStyle1 = new TableStyle() { Val = "GridTable4-Accent5" };
            TableWidth tableWidth1 = new TableWidth() { Width = "0", Type = TableWidthUnitValues.Auto };
            TableLook tableLook1 = new TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true
                , LastColumn = false, NoHorizontalBand = false, NoVerticalBand = true };
            TableCaption tableCaption1 = new TableCaption() { Val = "TABLE_NAME" };

            tableProperties1.Append(tableStyle1);
            tableProperties1.Append(tableWidth1);
            tableProperties1.Append(tableLook1);
            tableProperties1.Append(tableCaption1);
            #endregion

            TableGrid tableGrid1 = new TableGrid();
            #region -- COLUMN properties -----
            GridColumn gridColumn1 = new GridColumn() { Width = "4675" };
            #endregion
            tableGrid1.Append(gridColumn1);

            TableRow tableRow1 = new TableRow() { RsidTableRowAddition = "00391274", RsidTableRowProperties = "00391274" };
            #region -- TABLEROW properties -----
            TableRowProperties tableRowProperties1 = new TableRowProperties();
            ConditionalFormatStyle conditionalFormatStyle1 = new ConditionalFormatStyle() { Val = "100000000000"
                , FirstRow = true, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false
                , EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false
                , FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };
            tableRowProperties1.Append(conditionalFormatStyle1);
            #endregion
            tableRow1.Append(tableRowProperties1);

            TableCell tableCell1 = new TableCell();
            #region -- Table Cell Properties -----
            TableCellProperties tableCellProperties1 = new TableCellProperties();
            ConditionalFormatStyle conditionalFormatStyle2 = new ConditionalFormatStyle() { Val = "001000000000"
                , FirstRow = false, LastRow = false, FirstColumn = true, LastColumn = false, OddVerticalBand = false
                , EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false
                , FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };
            tableCellProperties1.Append(conditionalFormatStyle2);
            
            TableCellWidth tableCellWidth1 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };
            tableCellProperties1.Append(tableCellWidth1);
            #endregion
            tableCell1.Append(tableCellProperties1);

            Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };
            #region -- Paragraph Properties -----
            BookmarkStart bookmarkStart1 = new BookmarkStart() { Name = "_GoBack", Id = "0" };
            paragraph1.Append(bookmarkStart1);

            BookmarkEnd bookmarkEnd1 = new BookmarkEnd() { Id = "0" };
            paragraph1.Append(bookmarkEnd1);
            #endregion
            tableCell1.Append(paragraph1);

            #region -- Table Cell Properties -----
            TableCell tableCell2 = new TableCell();

            TableCellProperties tableCellProperties2 = new TableCellProperties();
            TableCellWidth tableCellWidth2 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties2.Append(tableCellWidth2);

            Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            ConditionalFormatStyle conditionalFormatStyle3 = new ConditionalFormatStyle() { Val = "100000000000", FirstRow = true, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            paragraphProperties1.Append(conditionalFormatStyle3);

            paragraph2.Append(paragraphProperties1);

            tableCell2.Append(tableCellProperties2);
            tableCell2.Append(paragraph2);
            #endregion

            tableRow1.Append(tableCell1);
            tableRow1.Append(tableCell2);

            #region -- TABLEROW properties -----
            TableRow tableRow2 = new TableRow() { RsidTableRowAddition = "00391274", RsidTableRowProperties = "00391274" };

            TableRowProperties tableRowProperties2 = new TableRowProperties();
            ConditionalFormatStyle conditionalFormatStyle4 = new ConditionalFormatStyle() { Val = "000000100000"
                , FirstRow = false, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false
                , EvenVerticalBand = false, OddHorizontalBand = true, EvenHorizontalBand = false, FirstRowFirstColumn = false
                , FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            tableRowProperties2.Append(conditionalFormatStyle4);

            #region -- Table Cell Properties -----
            TableCell tableCell3 = new TableCell();

            TableCellProperties tableCellProperties3 = new TableCellProperties();
            ConditionalFormatStyle conditionalFormatStyle5 = new ConditionalFormatStyle() { Val = "001000000000", FirstRow = false, LastRow = false, FirstColumn = true, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };
            TableCellWidth tableCellWidth3 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties3.Append(conditionalFormatStyle5);
            tableCellProperties3.Append(tableCellWidth3);
            Paragraph paragraph3 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            tableCell3.Append(tableCellProperties3);
            tableCell3.Append(paragraph3);
            #endregion

            #region -- Table Cell Properties -----
            TableCell tableCell4 = new TableCell();

            TableCellProperties tableCellProperties4 = new TableCellProperties();
            TableCellWidth tableCellWidth4 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties4.Append(tableCellWidth4);

            Paragraph paragraph4 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            ParagraphProperties paragraphProperties2 = new ParagraphProperties();
            ConditionalFormatStyle conditionalFormatStyle6 = new ConditionalFormatStyle() { Val = "000000100000", FirstRow = false, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = true, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            paragraphProperties2.Append(conditionalFormatStyle6);

            paragraph4.Append(paragraphProperties2);

            tableCell4.Append(tableCellProperties4);
            tableCell4.Append(paragraph4);
            #endregion
            tableRow2.Append(tableRowProperties2);
            tableRow2.Append(tableCell3);
            tableRow2.Append(tableCell4);
            #endregion

            #region -- TABLEROW properties -----
            TableRow tableRow3 = new TableRow() { RsidTableRowAddition = "00391274", RsidTableRowProperties = "00391274" };

            #region -- Table Cell Properties -----
            TableCell tableCell5 = new TableCell();

            TableCellProperties tableCellProperties5 = new TableCellProperties();
            ConditionalFormatStyle conditionalFormatStyle7 = new ConditionalFormatStyle() { Val = "001000000000", FirstRow = false, LastRow = false, FirstColumn = true, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };
            TableCellWidth tableCellWidth5 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties5.Append(conditionalFormatStyle7);
            tableCellProperties5.Append(tableCellWidth5);
            Paragraph paragraph5 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            tableCell5.Append(tableCellProperties5);
            tableCell5.Append(paragraph5);
            #endregion

            #region -- Table Cell Properties -----
            TableCell tableCell6 = new TableCell();

            TableCellProperties tableCellProperties6 = new TableCellProperties();
            TableCellWidth tableCellWidth6 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties6.Append(tableCellWidth6);

            Paragraph paragraph6 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            ParagraphProperties paragraphProperties3 = new ParagraphProperties();
            ConditionalFormatStyle conditionalFormatStyle8 = new ConditionalFormatStyle() { Val = "000000000000", FirstRow = false, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            paragraphProperties3.Append(conditionalFormatStyle8);

            paragraph6.Append(paragraphProperties3);

            tableCell6.Append(tableCellProperties6);
            tableCell6.Append(paragraph6);
            #endregion

            tableRow3.Append(tableCell5);
            tableRow3.Append(tableCell6);
            #endregion

            table1.Append(tableProperties1);
            table1.Append(tableGrid1);
            table1.Append(tableRow1);
            table1.Append(tableRow2);
            table1.Append(tableRow3);
            return table1;
        }

        public Table GenerateSimpleTable(ref DataTable _table)
        {
            Table table1 = new Table();

            #region -- TABLE properties -----
            TableProperties tableProperties1 = new TableProperties();
            TableStyle tableStyle1 = new TableStyle() { Val = "GridTable4-Accent5" };
            tableProperties1.Append(tableStyle1);
            #endregion

            TableGrid tableGrid1 = new TableGrid();
            for(int x = 0; x < _table.Rows.Count; x++)
            {
                GridColumn gridColumn1 = new GridColumn() { Width = "100" };
                tableGrid1.Append(gridColumn1);
            }

            TableRow tableRow1 = new TableRow() { RsidTableRowAddition = "00391274", RsidTableRowProperties = "00391274" };
            #region -- TABLEROW properties -----
            TableRowProperties tableRowProperties1 = new TableRowProperties();
            ConditionalFormatStyle conditionalFormatStyle1 = new ConditionalFormatStyle()
            {
                Val = "100000000000"
                ,
                FirstRow = true,
                LastRow = false,
                FirstColumn = false,
                LastColumn = false,
                OddVerticalBand = false
                ,
                EvenVerticalBand = false,
                OddHorizontalBand = false,
                EvenHorizontalBand = false,
                FirstRowFirstColumn = false
                ,
                FirstRowLastColumn = false,
                LastRowFirstColumn = false,
                LastRowLastColumn = false
            };
            tableRowProperties1.Append(conditionalFormatStyle1);
            #endregion
            tableRow1.Append(tableRowProperties1);

            TableCell tableCell1 = new TableCell();
            #region -- Table Cell Properties -----
            TableCellProperties tableCellProperties1 = new TableCellProperties();
            ConditionalFormatStyle conditionalFormatStyle2 = new ConditionalFormatStyle()
            {
                Val = "001000000000"
                ,
                FirstRow = false,
                LastRow = false,
                FirstColumn = true,
                LastColumn = false,
                OddVerticalBand = false
                ,
                EvenVerticalBand = false,
                OddHorizontalBand = false,
                EvenHorizontalBand = false,
                FirstRowFirstColumn = false
                ,
                FirstRowLastColumn = false,
                LastRowFirstColumn = false,
                LastRowLastColumn = false
            };
            tableCellProperties1.Append(conditionalFormatStyle2);

            TableCellWidth tableCellWidth1 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };
            tableCellProperties1.Append(tableCellWidth1);
            #endregion
            tableCell1.Append(tableCellProperties1);

            Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };
            #region -- Paragraph Properties -----
            BookmarkStart bookmarkStart1 = new BookmarkStart() { Name = "_GoBack", Id = "0" };
            paragraph1.Append(bookmarkStart1);

            BookmarkEnd bookmarkEnd1 = new BookmarkEnd() { Id = "0" };
            paragraph1.Append(bookmarkEnd1);
            #endregion
            tableCell1.Append(paragraph1);

            #region -- Table Cell Properties -----
            TableCell tableCell2 = new TableCell();

            TableCellProperties tableCellProperties2 = new TableCellProperties();
            TableCellWidth tableCellWidth2 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties2.Append(tableCellWidth2);

            Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();
            ConditionalFormatStyle conditionalFormatStyle3 = new ConditionalFormatStyle() { Val = "100000000000", FirstRow = true, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            paragraphProperties1.Append(conditionalFormatStyle3);

            paragraph2.Append(paragraphProperties1);

            tableCell2.Append(tableCellProperties2);
            tableCell2.Append(paragraph2);
            #endregion

            tableRow1.Append(tableCell1);
            tableRow1.Append(tableCell2);

            #region -- TABLEROW properties -----
            TableRow tableRow2 = new TableRow() { RsidTableRowAddition = "00391274", RsidTableRowProperties = "00391274" };

            TableRowProperties tableRowProperties2 = new TableRowProperties();
            ConditionalFormatStyle conditionalFormatStyle4 = new ConditionalFormatStyle()
            {
                Val = "000000100000"
                ,
                FirstRow = false,
                LastRow = false,
                FirstColumn = false,
                LastColumn = false,
                OddVerticalBand = false
                ,
                EvenVerticalBand = false,
                OddHorizontalBand = true,
                EvenHorizontalBand = false,
                FirstRowFirstColumn = false
                ,
                FirstRowLastColumn = false,
                LastRowFirstColumn = false,
                LastRowLastColumn = false
            };

            tableRowProperties2.Append(conditionalFormatStyle4);

            #region -- Table Cell Properties -----
            TableCell tableCell3 = new TableCell();

            TableCellProperties tableCellProperties3 = new TableCellProperties();
            ConditionalFormatStyle conditionalFormatStyle5 = new ConditionalFormatStyle() { Val = "001000000000", FirstRow = false, LastRow = false, FirstColumn = true, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };
            TableCellWidth tableCellWidth3 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties3.Append(conditionalFormatStyle5);
            tableCellProperties3.Append(tableCellWidth3);
            Paragraph paragraph3 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            tableCell3.Append(tableCellProperties3);
            tableCell3.Append(paragraph3);
            #endregion

            #region -- Table Cell Properties -----
            TableCell tableCell4 = new TableCell();

            TableCellProperties tableCellProperties4 = new TableCellProperties();
            TableCellWidth tableCellWidth4 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties4.Append(tableCellWidth4);

            Paragraph paragraph4 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            ParagraphProperties paragraphProperties2 = new ParagraphProperties();
            ConditionalFormatStyle conditionalFormatStyle6 = new ConditionalFormatStyle() { Val = "000000100000", FirstRow = false, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = true, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            paragraphProperties2.Append(conditionalFormatStyle6);

            paragraph4.Append(paragraphProperties2);

            tableCell4.Append(tableCellProperties4);
            tableCell4.Append(paragraph4);
            #endregion
            tableRow2.Append(tableRowProperties2);
            tableRow2.Append(tableCell3);
            tableRow2.Append(tableCell4);
            #endregion

            #region -- TABLEROW properties -----
            TableRow tableRow3 = new TableRow() { RsidTableRowAddition = "00391274", RsidTableRowProperties = "00391274" };

            #region -- Table Cell Properties -----
            TableCell tableCell5 = new TableCell();

            TableCellProperties tableCellProperties5 = new TableCellProperties();
            ConditionalFormatStyle conditionalFormatStyle7 = new ConditionalFormatStyle() { Val = "001000000000", FirstRow = false, LastRow = false, FirstColumn = true, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };
            TableCellWidth tableCellWidth5 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties5.Append(conditionalFormatStyle7);
            tableCellProperties5.Append(tableCellWidth5);
            Paragraph paragraph5 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            tableCell5.Append(tableCellProperties5);
            tableCell5.Append(paragraph5);
            #endregion

            #region -- Table Cell Properties -----
            TableCell tableCell6 = new TableCell();

            TableCellProperties tableCellProperties6 = new TableCellProperties();
            TableCellWidth tableCellWidth6 = new TableCellWidth() { Width = "4675", Type = TableWidthUnitValues.Dxa };

            tableCellProperties6.Append(tableCellWidth6);

            Paragraph paragraph6 = new Paragraph() { RsidParagraphAddition = "00391274", RsidRunAdditionDefault = "00391274" };

            ParagraphProperties paragraphProperties3 = new ParagraphProperties();
            ConditionalFormatStyle conditionalFormatStyle8 = new ConditionalFormatStyle() { Val = "000000000000", FirstRow = false, LastRow = false, FirstColumn = false, LastColumn = false, OddVerticalBand = false, EvenVerticalBand = false, OddHorizontalBand = false, EvenHorizontalBand = false, FirstRowFirstColumn = false, FirstRowLastColumn = false, LastRowFirstColumn = false, LastRowLastColumn = false };

            paragraphProperties3.Append(conditionalFormatStyle8);

            paragraph6.Append(paragraphProperties3);

            tableCell6.Append(tableCellProperties6);
            tableCell6.Append(paragraph6);
            #endregion

            tableRow3.Append(tableCell5);
            tableRow3.Append(tableCell6);
            #endregion

            table1.Append(tableProperties1);
            table1.Append(tableGrid1);
            table1.Append(tableRow1);
            table1.Append(tableRow2);
            table1.Append(tableRow3);
            return table1;
        }

        //public WordprocessingDocument OpenDocumentTemplate(string docName)
        //{

        //    // Open a WordprocessingDocument for editing using the filepath.
        //    WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(docName, true);

        //    // Assign a reference to the existing document body.
        //    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        //    // Add new text.
        //    Paragraph para = body.AppendChild(new Paragraph());
        //    Run run = para.AppendChild(new Run());
        //    run.AppendChild(new Text(txt));

        //}
    }
}
